package userManage.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import userManage.Model.dao.JsonUntil;
import userManage.Model.entity.user;
import userManage.Model.service.serviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet(name = "searchNumber", value = "/manage/userManage/searchNumber")
public class searchNumberservlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转化成String类
        String jsonString=String.valueOf(JsonUntil.getJson(req));
        //转换成JSONObject类
        JSONObject jsonObject= JSON.parseObject(jsonString);

        //获取搜索到的成员信息集合
        ArrayList<user> users = new ArrayList<>();
        try {
            users = serviceImpl.searchNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //向前端输出数据
        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();
        //如果该集合为空
        if (users.isEmpty()){
            object.put("code","-1");
            object.put("message","查询失败");
            out.println(object);
        }
        else{
            //序列化该集合
            String userList = JSONObject.toJSONString(users);
            object.put("users",userList);
            object.put("code","200");
            object.put("message","查询成功");
            out.println(object);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
